package org.hongqi.common.senpec.sys.dictionary;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hongqi.common.senpec.base.mybatisplus.IBaseServiceImpl;
import org.hongqi.common.senpec.base.sevlet.Result;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
@Slf4j
@RequiredArgsConstructor
public class DictionaryServiceImpl extends IBaseServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    private final DictionaryMapper dictionaryMapper;

    @Override
    public Result<Dictionary> mySave(Dictionary entity) {
        dictionaryMapper.insert(entity);
        return Result.ok(dictionaryMapper.selectById(entity.getId()));
    }

    @Override
    public Result<?> myRemoveById(Long id) {
        dictionaryMapper.deleteById(id);
        return Result.ok();
    }


    @Override
    public Result<Dictionary> myGetById(Long id) {
        return Result.ok(dictionaryMapper.selectById(id));
    }

    @Override
    public Result<Collection<Dictionary>> myListObjsWithoutPage(Dictionary entity) {

        return super.myListObjsWithoutPage(entity);
    }

    @Override
    public Result<Collection<Dictionary>> myListTree(Dictionary entity) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getTeam, entity.getTeam());
        lambdaQueryWrapper.eq(Dictionary::getPid, 0);
        lambdaQueryWrapper.orderByAsc(Dictionary::getSequence);
        return Result.ok(dictionaryMapper.selectList(lambdaQueryWrapper));
    }

    @Override
    public Collection<Dictionary> myListChildrenByPid(Long pid) {
        LambdaQueryWrapper<Dictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dictionary::getPid, pid);
        lambdaQueryWrapper.orderByAsc(Dictionary::getSequence);
        return dictionaryMapper.selectList(lambdaQueryWrapper);
    }
}
